home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / gdb / infptrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-01  |  10.7 KB  |  430 lines

  1. /* Low level Unix child interface to ptrace, for GDB when running under Unix.
  2.    Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "target.h"
  24.  
  25. #ifdef USG
  26. #include <sys/types.h>
  27. #endif
  28.  
  29. #include <syscall.h>
  30. #include <sys/param.h>
  31. #include <sys/dir.h>
  32. #include <signal.h>
  33. #include <sys/ioctl.h>
  34. #ifndef USG
  35. #include "ptrace.h"
  36. #endif
  37.  
  38. #if !defined (PT_KILL)
  39. #define PT_KILL 8
  40. #define PT_STEP 9
  41. #define PT_CONTINUE 7
  42. #define PT_READ_U 3
  43. #define PT_WRITE_U 6
  44. #define PT_READ_I 1
  45. #define    PT_READ_D 2
  46. #define PT_WRITE_I 4
  47. #define PT_WRITE_D 5
  48. #endif /* No PT_KILL.  */
  49.  
  50. #ifndef PT_ATTACH
  51. #define PT_ATTACH PTRACE_ATTACH
  52. #endif
  53. #ifndef PT_DETACH
  54. #define PT_DETACH PTRACE_DETACH
  55. #endif
  56.  
  57. #include "gdbcore.h"
  58. #ifndef    NO_SYS_FILE
  59. #include <sys/file.h>
  60. #endif
  61. #include <sys/stat.h>
  62.  
  63. #if !defined (FETCH_INFERIOR_REGISTERS)
  64. #include <sys/user.h>        /* Probably need to poke the user structure */
  65. #if defined (KERNEL_U_ADDR_BSD)
  66. #include <a.out.h>        /* For struct nlist */
  67. #endif /* KERNEL_U_ADDR_BSD.  */
  68. #endif /* !FETCH_INFERIOR_REGISTERS */
  69.  
  70.  
  71. /* This function simply calls ptrace with the given arguments.  
  72.    It exists so that all calls to ptrace are isolated in this 
  73.    machine-dependent file. */
  74. int
  75. call_ptrace (request, pid, addr, data)
  76.      int request, pid, *addr, data;
  77. {
  78.   return ptrace (request, pid, addr, data);
  79. }
  80.  
  81. #ifdef DEBUG_PTRACE
  82. /* For the rest of the file, use an extra level of indirection */
  83. /* This lets us breakpoint usefully on call_ptrace. */
  84. #define ptrace call_ptrace
  85. #endif
  86.  
  87. /* This is used when GDB is exiting.  It gives less chance of error.*/
  88.  
  89. void
  90. kill_inferior_fast ()
  91. {
  92.   if (inferior_pid == 0)
  93.     return;
  94.   ptrace (PT_KILL, inferior_pid, 0, 0);
  95.   wait ((int *)0);
  96. }
  97.  
  98. void
  99. kill_inferior ()
  100. {
  101.   kill_inferior_fast ();
  102.   target_mourn_inferior ();
  103. }
  104.  
  105. /* Resume execution of the inferior process.
  106.    If STEP is nonzero, single-step it.
  107.    If SIGNAL is nonzero, give it that signal.  */
  108.  
  109. void
  110. child_resume (step, signal)
  111.      int step;
  112.      int signal;
  113. {
  114.   errno = 0;
  115.  
  116.   /* An address of (int *)1 tells ptrace to continue from where it was. 
  117.      (If GDB wanted it to start some other way, we have already written
  118.      a new PC value to the child.)  */
  119.  
  120.   if (step)
  121. #if 0
  122. /*def NO_SINGLE_STEP*/
  123.     single_step (signal);
  124. #else    
  125.     ptrace (PT_STEP, inferior_pid, (int *)1, signal);
  126. #endif
  127.   else
  128. #ifdef AIX_BUGGY_PTRACE_CONTINUE
  129.     AIX_BUGGY_PTRACE_CONTINUE;
  130. #else
  131.     ptrace (PT_CONTINUE, inferior_pid, (int *)1, signal);
  132. #endif
  133.  
  134.   if (errno)
  135.     perror_with_name ("ptrace");
  136. }
  137.  
  138. #ifdef ATTACH_DETACH
  139. /* Nonzero if we are debugging an attached process rather than
  140.    an inferior.  */
  141. extern int attach_flag;
  142.  
  143. /* Start debugging the process whose number is PID.  */
  144. int
  145. attach (pid)
  146.      int pid;
  147. {
  148.   errno = 0;
  149.   ptrace (PT_ATTACH, pid, 0, 0);
  150.   if (errno)
  151.     perror_with_name ("ptrace");
  152.   attach_flag = 1;
  153.   return pid;
  154. }
  155.  
  156. /* Stop debugging the process whose number is PID
  157.    and continue it with signal number SIGNAL.
  158.    SIGNAL = 0 means just continue it.  */
  159.  
  160. void
  161. detach (signal)
  162.      int signal;
  163. {
  164.   errno = 0;
  165.   ptrace (PT_DETACH, inferior_pid, 1, signal);
  166.   if (errno)
  167.     perror_with_name ("ptrace");
  168.   attach_flag = 0;
  169. }
  170. #endif /* ATTACH_DETACH */
  171.  
  172. #if !defined (FETCH_INFERIOR_REGISTERS)
  173.  
  174. /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
  175.    to get the offset in the core file of the register values.  */
  176. #if defined (KERNEL_U_ADDR_BSD)
  177. /* Get kernel_u_addr using BSD-style nlist().  */
  178. CORE_ADDR kernel_u_addr;
  179.  
  180. void
  181. _initialize_kernel_u_addr ()
  182. {
  183.   struct nlist names[2];
  184.  
  185.   names[0].n_un.n_name = "_u";
  186.   names[1].n_un.n_name = NULL;
  187.   if (nlist ("/vmunix", names) == 0)
  188.     kernel_u_addr = names[0].n_value;
  189.   else
  190.     fatal ("Unable to get kernel u area address.");
  191. }
  192. #endif /* KERNEL_U_ADDR_BSD.  */
  193.  
  194. #if defined (KERNEL_U_ADDR_HPUX)
  195. /* Get kernel_u_addr using HPUX-style nlist().  */
  196. CORE_ADDR kernel_u_addr;
  197.  
  198. struct hpnlist {      
  199.         char *          n_name;
  200.         long            n_value;  
  201.         unsigned char   n_type;   
  202.         unsigned char   n_length;  
  203.         short           n_almod;   
  204.         short           n_unused;
  205. };
  206. static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
  207.  
  208. /* read the value of the u area from the hp-ux kernel */
  209. void _initialize_kernel_u_addr ()
  210. {
  211.     nlist ("/hp-ux", &nl);
  212.     kernel_u_addr = nl[0].n_value;
  213. }
  214. #endif /* KERNEL_U_ADDR_HPUX.  */
  215.  
  216. #if !defined (offsetof)
  217. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  218. #endif
  219.  
  220. /* U_REGS_OFFSET is the offset of the registers within the u area.  */
  221. #if !defined (U_REGS_OFFSET)
  222. #define U_REGS_OFFSET \
  223.   ptrace (PT_READ_U, inferior_pid, \
  224.       (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
  225. #endif
  226.  
  227. /* Registers we shouldn't try to fetch.  */
  228. #if !defined (CANNOT_FETCH_REGISTER)
  229. #define CANNOT_FETCH_REGISTER(regno) 0
  230. #endif
  231.  
  232. /* Fetch one register.  */
  233.  
  234. static void
  235. fetch_register (regno)
  236.      int regno;
  237. {
  238.   register unsigned int regaddr;
  239.   char buf[MAX_REGISTER_RAW_SIZE];
  240.   char mess[128];                /* For messages */
  241.   register int i;
  242.  
  243.   /* Offset of registers within the u area.  */
  244.   unsigned int offset;
  245.  
  246.   if (CANNOT_FETCH_REGISTER (regno))
  247.     {
  248.       bzero (buf, REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
  249.       supply_register (regno, buf);
  250.       return;
  251.     }
  252.  
  253.   offset = U_REGS_OFFSET;
  254.  
  255.   regaddr = register_addr (regno, offset);
  256.   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  257.     {
  258.       errno = 0;
  259.       *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, (int *)regaddr, 0);
  260.       regaddr += sizeof (int);
  261.       if (errno != 0)
  262.     {
  263.       sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
  264.       perror_with_name (mess);
  265.     }
  266.     }
  267.   supply_register (regno, buf);
  268. }
  269.  
  270.  
  271. /* Fetch all registers, or just one, from the child process.  */
  272.  
  273. void
  274. fetch_inferior_registers (regno)
  275.      int regno;
  276. {
  277.   if (regno == -1)
  278.     for (regno = 0; regno < NUM_REGS; regno++)
  279.       fetch_register (regno);
  280.   else
  281.     fetch_register (regno);
  282. }
  283.  
  284. /* Registers we shouldn't try to store.  */
  285. #if !defined (CANNOT_STORE_REGISTER)
  286. #define CANNOT_STORE_REGISTER(regno) 0
  287. #endif
  288.  
  289. /* Store our register values back into the inferior.
  290.    If REGNO is -1, do this for all registers.
  291.    Otherwise, REGNO specifies which register (so we can save time).  */
  292.  
  293. void
  294. store_inferior_registers (regno)
  295.      int regno;
  296. {
  297.   register unsigned int regaddr;
  298.   char buf[80];
  299.   extern char registers[];
  300.   register int i;
  301.  
  302.   unsigned int offset = U_REGS_OFFSET;
  303.  
  304.   if (regno >= 0)
  305.     {
  306.       regaddr = register_addr (regno, offset);
  307.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  308.     {
  309.       errno = 0;
  310.       ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  311.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  312.       if (errno != 0)
  313.         {
  314.           sprintf (buf, "writing register number %d(%d)", regno, i);
  315.           perror_with_name (buf);
  316.         }
  317.       regaddr += sizeof(int);
  318.     }
  319.     }
  320.   else
  321.     {
  322.       for (regno = 0; regno < NUM_REGS; regno++)
  323.     {
  324.       if (CANNOT_STORE_REGISTER (regno))
  325.         continue;
  326.       regaddr = register_addr (regno, offset);
  327.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  328.         {
  329.           errno = 0;
  330.           ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  331.               *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  332.           if (errno != 0)
  333.         {
  334.           sprintf (buf, "writing register number %d(%d)", regno, i);
  335.           perror_with_name (buf);
  336.         }
  337.           regaddr += sizeof(int);
  338.         }
  339.     }
  340.     }
  341. }
  342. #endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
  343.  
  344. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  345.    in the NEW_SUN_PTRACE case.
  346.    It ought to be straightforward.  But it appears that writing did
  347.    not write the data that I specified.  I cannot understand where
  348.    it got the data that it actually did write.  */
  349.  
  350. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  351.    to debugger memory starting at MYADDR.   Copy to inferior if
  352.    WRITE is nonzero.
  353.   
  354.    Returns the length copied, which is either the LEN argument or zero.
  355.    This xfer function does not do partial moves, since child_ops
  356.    doesn't allow memory operations to cross below us in the target stack
  357.    anyway.  */
  358.  
  359. int
  360. child_xfer_memory (memaddr, myaddr, len, write, target)
  361.      CORE_ADDR memaddr;
  362.      char *myaddr;
  363.      int len;
  364.      int write;
  365.      struct target_ops *target;        /* ignored */
  366. {
  367.   register int i;
  368.   /* Round starting address down to longword boundary.  */
  369.   register CORE_ADDR addr = memaddr & - sizeof (int);
  370.   /* Round ending address up; get number of longwords that makes.  */
  371.   register int count
  372.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  373.   /* Allocate buffer of that many longwords.  */
  374.   register int *buffer = (int *) alloca (count * sizeof (int));
  375.  
  376.   if (write)
  377.     {
  378.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  379.  
  380.       if (addr != memaddr || len < (int)sizeof (int)) {
  381.     /* Need part of initial word -- fetch it.  */
  382.         buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  383.       }
  384.  
  385.       if (count > 1)        /* FIXME, avoid if even boundary */
  386.     {
  387.       buffer[count - 1]
  388.         = ptrace (PT_READ_I, inferior_pid,
  389.               (int *)(addr + (count - 1) * sizeof (int)), 0);
  390.     }
  391.  
  392.       /* Copy data to be written over corresponding part of buffer */
  393.  
  394.       bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  395.  
  396.       /* Write the entire buffer.  */
  397.  
  398.       for (i = 0; i < count; i++, addr += sizeof (int))
  399.     {
  400.       errno = 0;
  401.       ptrace (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]);
  402.       if (errno)
  403.         {
  404.           /* Using the appropriate one (I or D) is necessary for
  405.          Gould NP1, at least.  */
  406.           errno = 0;
  407.           ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]);
  408.         }
  409.       if (errno)
  410.         return 0;
  411.     }
  412.     }
  413.   else
  414.     {
  415.       /* Read all the longwords */
  416.       for (i = 0; i < count; i++, addr += sizeof (int))
  417.     {
  418.       errno = 0;
  419.       buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  420.       if (errno)
  421.         return 0;
  422.       QUIT;
  423.     }
  424.  
  425.       /* Copy appropriate bytes out of the buffer.  */
  426.       bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  427.     }
  428.   return len;
  429. }
  430.